const fs = require('fs')  // Node.js文件操作模块
const Router = require('koa-router')
const router = new Router()

//自动读取server/api/routes文件夹下的所有API
const files = fs.readdirSync('./api/routes').filter((f)=>{
    return f.endsWith('.js')
})
for ( let f of files ){
    //获取文件
    let routes = require('./routes/' + f)
    //遍历文件接口输出的路径
    for( let {method, path, process} of routes ){
        switch(method){
            case 'post':
                router.post(path, process)
                break
            case 'put':
                router.put(path, process)
                break
            case 'delete':
                router.delete(path, process)
                break
            default:
                router.get(path, process)
        }
    }
}

module.exports = router